import { AudioClip, AudioSource, Component, _decorator, sys, assert, assetManager, resources } from 'cc';
import UrlEnum from '../Res/UrlEnum';
import TimerMgr from '../Timer/TimerMgr';
const { ccclass, property } = _decorator;
@ccclass('AudioMgr')
export default class AudioMgr extends Component {
    private LocalStorageKey_Bg: string = "bgMusicFlag";
    private LocalStorageKey_Effect: string = "effectMusicFlag";
    private effectOn: boolean;
    private bgOn: boolean;
    private audioSource: AudioSource;
    private audioClipDict: Object = {};
    start(): void {
        this.audioSource = this.node.getComponent(AudioSource)!;
        this.setDefaultSwitchState();
    }

    /**
     * 读取背景音乐和音效的设置
     */
    private setDefaultSwitchState(): void {
        let bgMusicFlag = sys.localStorage.getItem(this.LocalStorageKey_Bg);
        if (!bgMusicFlag) {
            this.bgOn = true;
        } else {
            this.bgOn = bgMusicFlag == "1";
        }
        this.setBgOn(this.bgOn);
        let effectMusicFlag = sys.localStorage.getItem(this.LocalStorageKey_Effect);
        if (!effectMusicFlag) {
            this.effectOn = true;
        } else {
            this.effectOn = effectMusicFlag == "1";
        }
    }

    /**
     * 播放音效
     * @param audioClip
     */
    public playEffect(audioClip: string): void {
        if (!this.effectOn) {
            return;
        }
        if (!audioClip) {
            return;
        }
        if (this.audioClipDict[audioClip]) {
            this.audioSource.playOneShot(this.audioClipDict[audioClip], 1);
        } else {
            assetManager.loadRemote<AudioClip>(`${UrlEnum.resUrl}res/audio/${audioClip}`, (err, asset: AudioClip) => {
                this.audioClipDict[audioClip] = asset;
                this.playEffect(audioClip);
            });
        }
    }
    public setMusicClip(audioClip: string): void {
        if (!audioClip) {
            return;
        }
        if (this.audioClipDict[audioClip]) {
            this.audioSource.clip = this.audioClipDict[audioClip];
            this.playBg();
        } else {
            assetManager.loadRemote<AudioClip>(`${UrlEnum.resUrl}res/audio/${audioClip}`, (err, asset: AudioClip) => {
                this.audioClipDict[audioClip] = asset;
                this.setMusicClip(audioClip);
            });
        }
    }
    /**
     * 播放背景音乐
     * @param key
     */
    public playBg(): void {
        this.audioSource.play();
    }

    /**
     * 停止背景音乐
     */
    public stopBg(): void {
        this.audioSource.stop();
    }
    public pauseBg(): void {
        this.audioSource.pause();
    }
    /**
     * 设置音效是否开启
     * @param $isOn
     */
    public setEffectOn($isOn: boolean): void {
        this.effectOn = $isOn;
        sys.localStorage.setItem(this.LocalStorageKey_Effect, $isOn ? "1" : "0");
    }

    /**
     * 设置背景音乐是否开启
     * @param $isOn
     */
    public setBgOn($isOn: boolean): void {
        this.bgOn = $isOn;
        sys.localStorage.setItem(this.LocalStorageKey_Bg, $isOn ? "1" : "0");

        if (!this.bgOn) {
            this.stopBg();
        } else {
            this.playBg();
        }
    }

    /**
     * 背景音乐是否已开启
     * @returns {boolean}
     */
    public get bgIsOn(): boolean {
        return this.bgOn;
    }

    /**
     * 音效是否已开启
     * @returns {boolean}
     */
    public get effectIsOn(): boolean {
        return this.effectOn;
    }


}